# Copyright (c) 2025 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

cmake_minimum_required(VERSION 3.14.1 FATAL_ERROR)
project(ark_guard)

set(TEST_TARGET ark_guard_test)

set(LIB_ARCH clang_x64)
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
    set(LIB_ARCH mingw_x86_64)
    add_definitions(-DPANDA_TARGET_WINDOWS)
endif ()

message("LIB_ARCH:${LIB_ARCH}")

set(ARK_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
set(ARK_COMPILER_TOOL ${ARK_ROOT}/../arkcompiler_tool)

# googletest
set(GOOGLE_TEST_PATH ${ARK_ROOT}/third_party/googletest)
include_directories(${GOOGLE_TEST_PATH}/googletest/include)
add_subdirectory(${GOOGLE_TEST_PATH} ${CMAKE_CURRENT_BINARY_DIR}/googletest)

# bounds_checking_function
include_directories(${ARK_ROOT}/third_party/bounds_checking_function/include)

# libabckit
add_definitions(-DABCKIT_USE_EXCEPTIONS)
set(LIBABCKIT_SRC_ROOT ${ARK_ROOT}/arkcompiler/runtime_core/libabckit)
set(LIBABCKIT_LIB_PATH ${ARK_ROOT}/out/x64.debug/arkcompiler/runtime_core)
set(THIRD_PARTY_LIB_PATH ${ARK_ROOT}/out/x64.debug/thirdparty)
include_directories(${LIBABCKIT_SRC_ROOT}/include)
link_directories(${LIBABCKIT_LIB_PATH})

# copy libs
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
    file(GLOB LIBABCKIT_LIBS ${LIBABCKIT_LIB_PATH}/*.dll)
else ()
    file(GLOB LIBABCKIT_LIBS ${LIBABCKIT_LIB_PATH}/*.so)
    file(GLOB_RECURSE THIRD_PARTY_LIBS ${THIRD_PARTY_LIB_PATH}/*.so)
    LIST(APPEND LIBABCKIT_LIBS ${THIRD_PARTY_LIBS})

    file(COPY ${ARK_COMPILER_TOOL}/${LIB_ARCH}/libark_jsruntime.so DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
    file(COPY ${ARK_COMPILER_TOOL}/${LIB_ARCH}/libace_napi.so DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
    file(COPY ${ARK_COMPILER_TOOL}/${LIB_ARCH}/libarktscompiler.so DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
    file(COPY ${ARK_COMPILER_TOOL}/${LIB_ARCH}/libarkaotmanager.so DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
    file(COPY ${ARK_COMPILER_TOOL}/${LIB_ARCH}/libarktsziparchive.so DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
    file(COPY ${ARK_COMPILER_TOOL}/${LIB_ARCH}/libarktarget_options.so DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
    file(COPY ${ARK_COMPILER_TOOL}/${LIB_ARCH}/libuv.so DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
    file(COPY ${ARK_COMPILER_TOOL}/${LIB_ARCH}/libhmicui18n.so DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
    file(COPY ${ARK_COMPILER_TOOL}/${LIB_ARCH}/libhmicuuc.so DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
    file(COPY ${ARK_COMPILER_TOOL}/${LIB_ARCH}/libarktsfile.so DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
    file(COPY ${ARK_COMPILER_TOOL}/${LIB_ARCH}/libarkencoder.so DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
    file(COPY ${ARK_COMPILER_TOOL}/${LIB_ARCH}/libsec_shared.so DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
endif ()
file(COPY ${LIBABCKIT_LIBS} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

# abckit_wrapper
include_directories(abckit_wrapper/include)
add_subdirectory(abckit_wrapper)

# ark_guard static library
include_directories(include)
include_directories(library)
include_directories(abckit_wrapper/tests/mock/include)
include_directories(abckit_wrapper/tests/mock/include/libarkbase)
file(GLOB SRC_MOCK abckit_wrapper/tests/mock/libarkbase/*.cpp)
file(GLOB_RECURSE ark_guard_library_sources library/*.cpp)
add_library(ark_guard_static STATIC ${ark_guard_library_sources} ${SRC_MOCK})
target_link_libraries(ark_guard_static abckit_wrapper)

# ark guard
add_executable(${PROJECT_NAME} ark_guard/main.cpp)
target_link_libraries(${PROJECT_NAME} ark_guard_static)

# test
add_definitions(-DARK_GUARD_ABC_FILE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests/build/")
add_definitions(-DARK_GUARD_UNIT_TEST_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests/")
add_definitions(-DARK_GUARD_ETS_STD_LIB="${ARK_COMPILER_TOOL}/plugins/ets/etsstdlib.abc")

include_directories(tests/util/include)
file(GLOB_RECURSE test_sources tests/ut/*.cpp)
file(GLOB_RECURSE test_util_sources tests/util/*.cpp)
add_executable(${TEST_TARGET} tests/main.cpp ${test_sources} ${test_util_sources})
target_link_libraries(${TEST_TARGET}
        gtest
        gmock
        abckit
        ark_guard_static
)

if (NOT CMAKE_SYSTEM_NAME STREQUAL "Windows")
target_link_libraries(${TEST_TARGET} ${ARK_COMPILER_TOOL}/${LIB_ARCH}/libarktsbase.so)
target_link_libraries(${TEST_TARGET} ${ARK_COMPILER_TOOL}/${LIB_ARCH}/libarkruntime.so)
endif ()
